<?php defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

/* @version $Id: xmlimport.class.php 1126 2008-06-09 05:45:04Z julienv $
 * @package Joomleague  
 * @copyright Copyright (C) 2007  JoomLeague
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see _joomleague_license.txt
 *   
 * JoomLeague League Management and Prediction Game for Joomla!
 *  
 * Homepage: http://www.joomleague.de
 * Support: htt://www.joomleague.de/forum/
 * 
 * This file is part of JoomLeague.
 *  
 * JoomLeague is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * Please note that the GPL states that any headers in files and
 * Copyright notices as well as credits in headers, source files
 * and output (screens, prints, etc.) can not be removed.
 * You can extend them with your own credits, though...
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * The "GNU General Public License" (GPL) is available at
 * http://www.gnu.org/copyleft/gpl.html.
*/ 
 
/**
 * Class XmlImport
 *
 * Parse a Joomleague XML project document.
 * @author Julien Vonthron Ivan <julien.vonthron@gmail.com>
 * @since PHP5.1
 * @package Joomleague
 * @licence GNU GPL
 */

class XmlImport
{
	/**
   * Xml parser container.
   *
   * @var resource parser
   */
  var $parser;
  
  /**
   * Extracted objects
   *
   * @var array objects
   */
  var $objects = array();

  /**
   * current object
   *
   * @access private
   * @var object
   */
  var $_object = null;
  
  /**
   * current property
   * 
   * @access private
   * @var string
   */
  var $_property = "";
  
  /**
   * property data
   *
   * @access private
   * @var string
   */
  var $_data = "";
  
  /**
   * Error message
   *
   * @access private
   * @var string
   */
  var $_error = "";

  function XmlImport()
  {
  	$this->parser = xml_parser_create();

  	xml_set_object($this->parser, $this);
  	xml_set_element_handler($this->parser, "tag_open", "tag_close");
  	xml_set_character_data_handler($this->parser, "cdata");
  }

  function parse( $src, $encoding = 'UTF-8' )
  {  	
    xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);
    xml_parser_set_option($this->parser, XML_OPTION_TARGET_ENCODING, $encoding);
    
  	if (!($fp = fopen($src, "r"))) print 'couldn\'t open file '. $src;

  	//loop through data
  	while ($data = fread($fp, 4096)) {
  		//parse the fragment
  		xml_parse($this->parser, $data, feof($fp));
  	}
  	
  	fclose($fp);
    return $this->_objects;
  }

  function tag_open($parser, $tag, $attributes)
  {
  	global $database;
    if ( $tag == "record" )
  	{
  	  /**
  	   * instantiate corresponding joomleague object
  	   */
      $className = $attributes['object'];
      $this->_object = new $className($database);
    }
    else if ( $this->_object )
    {
      $this->_property = $tag;
      $this->_data = '';
    }
  }

  function cdata($parser, $cdata)
  {
  	if ( $this->_object && $this->_property )
  	{
      /**
       * cdata might be split
       */
  	  $this->_data .= $cdata;
    }
  }

  function tag_close($parser, $tag)
  {
  	if ( $tag == "record" )
  	{
      // end of object, store it.
      $this->_objects[ strtolower(get_class( $this->_object )) ][] = $this->_object;
      $this->_object = null;
    }
    else if ( $this->_property == $tag )
    {
      // store value in object
      $property = $this->_property;
      if ( array_key_exists( $property, get_class_vars(get_class( $this->_object )) ) ) {
        $this->_object->$property = $this->_data;        
      }
      // reset property name and value
      $this->_property = '';
      $this->_data = '';
    }
  }
}

?>
